﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Health : MonoBehaviour {

    [Header("最大血量"),SerializeField()]
    private int maxHP;//最大血量
    [Header("销毁时间,都需要修改这个值")]
    public int desTime;//销毁时间

    [HideInInspector]
    public int currentHP;//当前血量

    Animator getAnimator;//获得当前对象的Animator
    void Reset()//重置血量
    {
        currentHP = maxHP;
    }
    void Start () {
        getAnimator = GetComponentInChildren<Animator>();
        currentHP = maxHP;
    }

	void Update () {
        if (currentHP <= 0)
        {
            getAnimator.SetTrigger("death");//触发死亡动画
            Destroy(gameObject,desTime);
        } 
	}
    /// <summary>
    /// 受伤减血的方法
    /// </summary>
    /// <param name="hurtValue">给予的伤害值</param>
    public void FeelHurt(int hurtValue) {
        //getAnimator.SetTrigger("hurt");//触发受伤动画
        currentHP -= hurtValue;
    }
    /// <summary>
    /// 恢复血量方法
    /// </summary>
    /// <param name="resValue">恢复血量的值</param>
    public void Restort(int resValue) {
        if ((currentHP + resValue) > maxHP)
        {
            currentHP = maxHP;
        }
        else {
            currentHP += resValue;
        }
    }
}
